package github.kawaiior.yuanchuxiuzhen.core.init;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.world.structures.Structure_01;
import github.kawaiior.yuanchuxiuzhen.common.world.structures.Structure_02;
import github.kawaiior.yuanchuxiuzhen.common.world.structures.Structure_03;
import github.kawaiior.yuanchuxiuzhen.common.world.structures.Structure_04;
import github.kawaiior.yuanchuxiuzhen.common.world.structures.Structure_09;
import github.kawaiior.yuanchuxiuzhen.common.world.structures.Structure_10;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.gen.feature.NoFeatureConfig;
import net.minecraft.world.gen.feature.structure.Structure;
import net.minecraft.world.gen.settings.DimensionStructuresSettings;
import net.minecraft.world.gen.settings.StructureSeparationSettings;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.HashMap;
import java.util.Map;

public class StructureInit {

    public static final DeferredRegister<Structure<?>> STRUCTURES =
            DeferredRegister.create(ForgeRegistries.STRUCTURE_FEATURES, Yuanchuxiuzhen.MODID);

    // /locate yuanchuxiuzhen:structure_01
    // /setblock ~ ~ ~ minecraft:chest{LootTable:"yuanchuxiuzhen:chests/structure_01"}
    // /setblock ~ ~ ~ minecraft:chest{LootTable:"yuanchuxiuzhen:chests/structure_04"}
    // /setblock ~ 79 ~ minecraft:chest[facing=south]{LootTable:"yuanchuxiuzhen:chests/structure_10"}

    // 棺材?
    public static final RegistryObject<Structure<NoFeatureConfig>> STRUCTURE_01 =
            STRUCTURES.register("structure_01", Structure_01::new);
    // 地府传送门?
    public static final RegistryObject<Structure<NoFeatureConfig>> STRUCTURE_02 =
            STRUCTURES.register("structure_02", Structure_02::new);
    // 地府传送门的另一部分?
    public static final RegistryObject<Structure<NoFeatureConfig>> STRUCTURE_03 =
            STRUCTURES.register("structure_03", Structure_03::new);
    // 一个平台？(改了改)
    public static final RegistryObject<Structure<NoFeatureConfig>> STRUCTURE_04 =
            STRUCTURES.register("structure_04", Structure_04::new);
    // 太极？
    public static final RegistryObject<Structure<NoFeatureConfig>> STRUCTURE_09 =
            STRUCTURES.register("structure_09", Structure_09::new);
    // 小屋？（也不算小）
    public static final RegistryObject<Structure<NoFeatureConfig>> STRUCTURE_10 =
            STRUCTURES.register("structure_10", Structure_10::new);

    public static void setupStructures() {
        setupMapSpacingAndLand(STRUCTURE_01.get(),
                new StructureSeparationSettings(35,5, 1234567890),
                true);
        setupMapSpacingAndLand(STRUCTURE_02.get(),
                new StructureSeparationSettings(70, 30, 1345678901),
                true);
        setupMapSpacingAndLand(STRUCTURE_03.get(),
                new StructureSeparationSettings(70, 30, 234565781),
                true);
        setupMapSpacingAndLand(STRUCTURE_04.get(),
                new StructureSeparationSettings(70, 30, 334565781),
                true);
        setupMapSpacingAndLand(STRUCTURE_09.get(),
                new StructureSeparationSettings(70, 30, 334565781),
                true);
        setupMapSpacingAndLand(STRUCTURE_10.get(),
                new StructureSeparationSettings(70, 30, 934565781),
                true);
    }

    public static <F extends Structure<?>> void setupMapSpacingAndLand(F structure, StructureSeparationSettings structureSeparationSettings,
                                                                       boolean transformSurroundingLand) {
        Structure.NAME_STRUCTURE_BIMAP.put(structure.getRegistryName().toString(), structure);

        if (transformSurroundingLand) {
            Structure.field_236384_t_ = ImmutableList.<Structure<?>>builder()
                    .addAll(Structure.field_236384_t_)
                    .add(structure)
                    .build();
        }

        DimensionStructuresSettings.field_236191_b_ =
                ImmutableMap.<Structure<?>, StructureSeparationSettings>builder()
                        .putAll(DimensionStructuresSettings.field_236191_b_)
                        .put(structure, structureSeparationSettings)
                        .build();

        WorldGenRegistries.NOISE_SETTINGS.getEntries().forEach(settings -> {
            Map<Structure<?>, StructureSeparationSettings> structureMap =
                    settings.getValue().getStructures().func_236195_a_();

            if (structureMap instanceof ImmutableMap) {
                Map<Structure<?>, StructureSeparationSettings> tempMap = new HashMap<>(structureMap);
                tempMap.put(structure, structureSeparationSettings);
                settings.getValue().getStructures().func_236195_a_();

            } else {
                structureMap.put(structure, structureSeparationSettings);
            }
        });
    }
}
